package messengerserver;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import messengercommon.person.Contact;
import messengercommon.person.EnumStatus;
import messengercommon.person.Person;

/**
 *
 * @author Samus
 */
public class UserAdmin {

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2databasePU");
    EntityManager em = factory.createEntityManager();
    private static UserAdmin instance = new UserAdmin();

    private UserAdmin() {
    }

    public static UserAdmin getInstance() {
        return instance;
    }

    public int autentifyUser(Person pers) throws Exception {
        int res = 0;
        Person person = null;
        try {
            person = (Person) em.createNamedQuery("buscarPersonPorNombre").setParameter("username", pers.getUsername()).getSingleResult();
            if (person.getPassword().equals(pers.getPassword())) {
                res = 1;
            }
        } catch (Exception ex) {
            res = 2;
            System.out.println("No existe la persona " + pers.getUsername());
        }
        return res;
    }

    public int autentifyContact(String nomb) throws Exception {
        int res = 0;
        Contact contac = null;
        try {
            contac = (Contact) em.createNamedQuery("buscarContactPorNombre").setParameter("username", nomb).getSingleResult();
            contac.setStatus(null);
            if (contac.getUsername().equals(nomb)) {
                res = 2;
            }
        } catch (Exception ex) {
            res = 0;
            System.out.println("No existe el contacto " + nomb);
        }
        return res;
    }

    public Person readUser(String nomb) throws Exception {
        Person person = null;
        try {
            person = (Person) em.createNamedQuery("buscarPersonPorNombre").setParameter("username", nomb).getSingleResult();
            person.setStatus(null);
        } catch (Exception ex) {
            throw new Exception("No existe la persona " + nomb);
        }
        return person;
    }

    public Contact readContact(String nomb) throws Exception {
        Contact contact = null;
        try {
            contact = (Contact) em.createNamedQuery("buscarPersonPorNombre").setParameter("username", nomb).getSingleResult();
            contact.setStatus(null);
        } catch (Exception ex) {
            throw new Exception("No existe el contacto " + nomb);
        }
        return contact;
    }

    public boolean persistUser(Person person) throws Exception {
        boolean res = false;
        Person aux = null;
        try {
            aux = readUser(person.getUsername());
        } catch (Exception ex) {
        }
        if (aux != null) {
            try {
                em.getTransaction().begin();
                aux = readUser(person.getUsername());
                aux.setContacts(person.getContacts());
                aux.setNick(person.getNick());
                em.persist(aux);
                em.getTransaction().commit();
                res = true;
            } catch (Exception ex) {
                System.out.println("Error al actualizar los datos");
            }
        } else {
            try {
                em.getTransaction().begin();
                Person p = new Person();
                p.setUsername(person.getUsername());
                p.setPassword(person.getPassword());
                p.setNick(person.getNick());
                em.persist(p);
                em.getTransaction().commit();
                res = true;
            } catch (Exception ex) {
                System.out.println("Error al ingresar una nueva persona");
            }
        }
        return res;
    }

    public boolean persistContact(Person person, Contact contact) throws Exception {
        boolean res = false;
        try {
            em.getTransaction().begin();
            Person aux = readUser(person.getUsername());
            aux.getContacts().add(contact);
            em.getTransaction().commit();
            res = true;
        } catch (Exception ex) {
            throw new Exception(ex.toString());
        }
        return res;
    }
}
